package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

@Autowired
private AddressBookMapper addressBookMapper;

        public List<AddressBook> list(){
                Long userId= BaseContext.getCurrentId();
                List<AddressBook> addressBooks = addressBookMapper.list_addressBook(userId);
        return  addressBooks;
        }

        public void save(AddressBook addressBook) {
                addressBook.setUserId(BaseContext.getCurrentId());
                addressBookMapper.save(addressBook);
        }

        public AddressBook defaults(){
                Long userId= BaseContext.getCurrentId();
                AddressBook addressBook = addressBookMapper.defaults(userId);
                return addressBook;
        }

        public void update(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
        }

        public AddressBook getById(Long id){
                AddressBook addressBook=addressBookMapper.getById(id);
                return addressBook;
        }

        public void delete(Long id){
                addressBookMapper.deleteById(id);
        }
        public  void setDefault(Long id){
                //设置所有地址为非默认
                Long userId= BaseContext.getCurrentId();
                addressBookMapper.setAllDefaultFalse(userId);
                //设置当前地址为默认
                addressBookMapper.setDefault(id);
        }

}
